package com.hzy.fivepalace.wx.scheduler;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzy.fivepalace.db.domain.DtsGroupOpenList;
import com.hzy.fivepalace.db.service.DtsGroupOpenListService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @创建者：zhouwei
 * @创建时间：2023/10/26
 * @描述：  定时任务
 */
@Component
public class DataAnalysis {

    @Resource
    private DtsGroupOpenListService groupOpenListService;


    /**
     *  拼团订单的成团状态 查询
     *  每5分钟执行一次
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void checkGroupOpenOrder() {
        try {
            QueryWrapper<DtsGroupOpenList> qw = new QueryWrapper<>();
            qw.eq("state", 0);
            List<DtsGroupOpenList> orderList = groupOpenListService.list(qw);
            if (CollUtil.isNotEmpty(orderList)) {
                for (DtsGroupOpenList openList : orderList) {
                    //超过12个小时之后 未成团的 自动退款
                    if(openList.getEndTime().getTime() < new Date().getTime()){
                        groupOpenListService.refundOpenList(openList);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
